package com.wing.productionPlan.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.Idempotent;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.request.ProfessionForm;
import com.wing.productionPlan.model.response.ProfessionVO;
import com.wing.productionPlan.service.ProfessionService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@Validated
@Api(value = "工种管理", tags = "#/product/professionList")
@RestController("ProfessionController")
@RequestMapping("productionPlan/admin/profession")
@RequiredArgsConstructor
public class ProfessionController {


    private final ProfessionService professionService;


    @ApiOperation(value = "工种列表", notes = "搜索列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProfessionVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String professionName
            ,String professionCode
            ,String notInListIds


    ) {
        Page<ProfessionVO> pg = new Page<>(page, limit);
        HashMap<String,Object> map = new HashMap();
        map.put("professionName", professionName);
        map.put("professionCode", professionCode);
        if (org.apache.commons.lang.StringUtils.isNotBlank(notInListIds)) {
            String[] lists = notInListIds.split(",");
            map.put("notInListIds", lists);
        }

        Page<ProfessionVO> list = professionService.list4app(pg, map);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/listByName")
    public JsonResult listByName(String professionName) {
        HashMap<String,Object> map = new HashMap();
        map.put("professionName", professionName);
        return JsonResult.success(professionService.list4app(map));
    }




    @ApiOperation(value = "添加工种", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/professionAdd")
    @Idempotent(keyPrefix = "deliveryUpdate")
    public JsonResult add(@RequestBody ProfessionForm professionForm) {
        if(StringUtils.isBlank(professionForm.getProfessionName())){
            return JsonResult.fail("工种名称没有填写");
        }
        if(StringUtils.isBlank(professionForm.getProfessionCode())){
            return JsonResult.fail("工种编码没有填写");
        }

        return professionService.add(professionForm);

    }


    @ApiOperation(value = "修改工种", notes = "修改")
    @PostMapping("/professionUpdate")
    public JsonResult update(@RequestBody ProfessionForm professionForm) {
        if(StringUtils.isBlank(professionForm.getProfessionName())){
            return JsonResult.fail("工种名称没有填写");
        }
        if(StringUtils.isBlank(professionForm.getProfessionCode())){
            return JsonResult.fail("工种编码没有填写");
        }
        return professionService.edit(professionForm);
    }


    @ApiOperation(value = "获取当前工种")
    @GetMapping("/get")
    public JsonResult get(Long id) {
        return JsonResult.success(professionService.getById(id));
    }

    @ApiOperation(value = "状态启用禁用", notes = "修改")
    @PostMapping("/professionEditStatus")
    public JsonResult editStatus(@RequestBody ProfessionForm professionForm) {
       if (professionService.editStatus(professionForm)> 0) {
            return JsonResult.success("操作成功！");
        }
        return JsonResult.fail("操作失败！");
    }


    @ApiOperation(value = "查询启用的工种列表", notes = "查询")
    @GetMapping("/getAllProfession")
    public JsonResult getAllProfession() {
        return JsonResult.success(professionService.getAllProfession());
    }

}
